package server;

import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import client.Configuration;

public class BankFixedAccount extends BankAccount implements BkFAccITF{

	/**
	 * @param args
	 */
	/*int FANum;
	double FASum;
	Date startTime;
	int limit;
	double FAinterest;*/
	
	
	BankAccList accList = new BankAccList();
	BankFAccList faccList = new BankFAccList();
	BankCurrentAccount BkCAcc = new BankCurrentAccount();
	
	
	public BankFixedAccount() throws RemoteException{
		// TODO Auto-generated constructor stub
	}

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	public int addFA(int pID,double FASum,int limit,Date startTime){
		return faccList.addFA(pID, FASum, limit,startTime);
	}
	
	public double checkInterest(int pID,int FANum){
		double FAinterest=0;
	    if(faccList.getLimit(pID, FANum)==1)
			FAinterest = faccList.getFASum(pID,FANum)*0.0225;
		else if(faccList.getLimit(pID, FANum)==3)
			FAinterest = faccList.getFASum(pID,FANum)*0.0333*3;
		else if(faccList.getLimit(pID, FANum)==5)
			FAinterest = faccList.getFASum(pID,FANum)*0.036*5;
		return FAinterest;			
	}
	
	
	public void withdrawl(int pID,int FANum){
		    double balance = BkCAcc.checkBalance(pID);
		    int limit=faccList.getLimit(pID, FANum);
		    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		    Date start=null;
		    try {
				start = dateFormat.parse(getStartTime(pID,FANum));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    Calendar ctime=Calendar.getInstance();
		    ctime.setTime(start);
		    ctime.add(Calendar.YEAR, limit);
		    Calendar now=Calendar.getInstance();
		    if(now.after(ctime)==true){
		    double total=faccList.getFASum(pID,FANum)+checkInterest(pID,FANum); 
		    balance+=total;
		    }
		    else
		    	 balance+=faccList.getFASum(pID,FANum);
		    accList.setCABalance(accList.getCANum(pID), balance);
			faccList.deleFA(pID,FANum);
			}
	
	
	public double getFASum(int pID,int FANum){
		return faccList.getFASum(pID, FANum);
	}
	
	public String getStartTime(int pID,int FANum){
		return faccList.getStartTime(pID, FANum);
	}
	
	public int getLimit(int pID,int FANum){
		return faccList.getLimit(pID, FANum);
	}
	
	public int getSize(){
		return faccList.getSize();
	}
	
	public int getFirstFANum(int pID){
		return faccList.getFirstFANum(pID);
	}
	
	public int getNextFANum(int pID,int faNum){
		return faccList.getNextFANum(pID,faNum);
	}


	@Override
	public int getpSize(int pID) throws RemoteException {
		// TODO Auto-generated method stub
		return faccList.getpSize(pID);
	}
}
